package com.wqh.common.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author wanqh
 * @date 2018/02/10 15:54
 * @description:
 */
public class HttpUtils {
    public static String get(String uri) {
        String result = null;
        try {
            HttpGet httpGet = new HttpGet(uri);
            HttpClient build = HttpClientBuilder.create().build();
            HttpResponse response = build.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result= EntityUtils.toString(response.getEntity(),"utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String uri = "http://wthrcdn.etouch.cn/weather_mini?citykey=101280204";
        String s = HttpUtils.get(uri);
        System.out.println(s);
    }
}
